package com.demo.work.main;

import com.demo.work.util.StringUtils;
import com.demo.work.util.XSSFWorkbookWrapper;
import lombok.Data;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Optional;

/**
 * @author lyf
 */
@Data
public class CreateWeekReport {

    private LocalDate now = LocalDate.now();
    private String path = "D:\\Users\\liuya\\Documents\\工作资料\\周报";

    public static void main(String[] args) {
        new CreateWeekReport().createNewWeekReport();
    }

    private void createNewWeekReport() {
        File folder = new File(path);
        File[] files = folder.listFiles();

        if (files == null) {
            return ;
        }

        Optional<File> lastFile = Arrays.stream(files).filter(e -> e.getName().contains(".xlsx")).max((o1, o2) -> {
            try {
                BasicFileAttributes b = Files.readAttributes(o1.toPath(), BasicFileAttributes.class);
                BasicFileAttributes b2 = Files.readAttributes(o2.toPath(), BasicFileAttributes.class);
                return b.lastModifiedTime().compareTo(b2.lastModifiedTime());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return 0;
        });

        if (lastFile.isPresent()) {
            File file = lastFile.get();
            System.out.println("get file\t" + file.getName());
            String newPath = file.getParent();
            XSSFWorkbookWrapper x = new XSSFWorkbookWrapper(file);
            x.useSheet("Sheet1")
                    .setValue("G", 0, String.format("滚动工作周报(%s---%s)",
                            now.with(DayOfWeek.MONDAY).format(DateTimeFormatter.ofPattern("yyyy/MM/dd")),
                            now.with(DayOfWeek.SUNDAY).format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))))
                    .setValue("B", 2, String.format("上周(%s---%s)",
                            now.minusWeeks(1).with(DayOfWeek.MONDAY).format(DateTimeFormatter.ofPattern("yyyy/MM/dd")),
                            now.minusWeeks(1).with(DayOfWeek.SUNDAY).format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))))
                    .setValue("G", 2, String.format("本周(%s---%s)",
                            now.with(DayOfWeek.MONDAY).format(DateTimeFormatter.ofPattern("yyyy/MM/dd")),
                            now.with(DayOfWeek.SUNDAY).format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))))
                    .setValue("L", 2, String.format("下周(%s---%s)",
                            now.plusWeeks(1).with(DayOfWeek.MONDAY).format(DateTimeFormatter.ofPattern("yyyy/MM/dd")),
                            now.plusWeeks(1).with(DayOfWeek.SUNDAY).format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))))
            ;

            String s = file.getName().replaceAll("[\\d]{8}-[\\d]{8}", String.format("%s-%s",
                    now.with(DayOfWeek.MONDAY).format(DateTimeFormatter.ofPattern("yyyyMMdd")),
                    now.with(DayOfWeek.SUNDAY).format(DateTimeFormatter.ofPattern("yyyyMMdd"))));
            String join = StringUtils.join(Arrays.asList(newPath, s), File.separator);
            System.out.println("output file\t" + join);

            x.save(new File(join));
        }
    }
}
